<route lang="json5" type="home">
{
  style: {
    navigationBarTitleText: '首页',
    navigationBarTextStyle: 'white',
    navigationStyle: 'custom',
  },
}
</route>

<script lang="ts" setup>
import { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/api/home'
import { XtxGuessInstance } from '@/types/components-instance'
import PageSkeleton from './components/PageSkeleton.vue'

// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  bannerList.value = res.result
}

// 获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
const getCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  console.log(res)
  categoryList.value = res.result
}

// 获取热门推荐数据
const hotList = ref<HotItem[]>([])
const getHotData = async () => {
  const res = await getHomeHotAPI()
  console.log(res)
  hotList.value = res.result
}

// 数据加载标记
const isLoading = ref(false)

// 页面加载
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryData(), getHotData()])
  isLoading.value = false
})

// 获取猜你喜欢组件的实例对象
const guessRef = ref<XtxGuessInstance>()

// 滚动触底
const onScrolltolower = () => {
  // console.log('滚动触底')
  guessRef.value?.getMore()
}

// 自定义下拉刷新动画
const isTriggered = ref(false)

// 自定义下拉刷新触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  console.log('自定义下拉刷新触发')
  // getHomeBannerData()
  // getCategoryData()
  // getHotData()
  // 重置猜你喜欢的数据
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getCategoryData(),
    getHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view
    class="scroll-view"
    scroll-y
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐-->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
      <view>index</view>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  .scroll-view {
    flex: 1;
  }
}
</style>
